﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace BenevolentSun
{
    class LevelSelectionScreen : Screen
    {

        TextMenu menu;


        public override void LoadContent()
        {
            SpriteFont font = Content.Load<SpriteFont>("Fonts/Ebrima30");
            menu = new TextMenu();
            menu.SelectedColor = new Color(204, 204, 204, (byte)(1 * 255));
            menu.UnselectedColor = new Color(51, 51, 51, (byte)(1 * 255));
            for (int i = 0; i < Session.Level; ++i)
            {
                menu.AddEntry(new TextField("Stage: " + (i + 1) + " - Cleared", font));
            }
            if (Session.IsGameComplete == false)
                menu.AddEntry(new TextField("Stage: " + Session.Level, font));
            menu.SetPositions(new Vector2(0, 100), new Vector2(0, 30));

            base.LoadContent();
        }


        public override void HandleInput()
        {
            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadDown) ||
                InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.LeftThumbstickDown) ||
                InputManager.IsNewKeyPress(Keys.Down))
            {
                menu.Next();
            }
            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.DPadUp) ||
                InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.LeftThumbstickUp) ||
                InputManager.IsNewKeyPress(Keys.Up))
            {
                menu.Prev();
            }


            if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.B) ||
                InputManager.IsNewKeyPress(Keys.Escape))
            {
                // open the level here
                ScreenManager.AddScreen(new MainMenuScreen());
                ExitScreen();
            }
            else if (InputManager.IsNewButtonPress(PlayerIndex.One, Buttons.A) ||
                InputManager.IsNewKeyPress(Keys.Enter))
            {
                int levelNum = menu.Entries.IndexOf(menu.Selected);
                // open the level here
                ScreenManager.AddScreen(new LevelScreen(levelNum));
                ExitScreen();
            }

            base.HandleInput();
        }


        public override void Update(GameTime gameTime, bool isFocusAvailable, bool isOccluded)
        {
            menu.Update(gameTime);
            base.Update(gameTime, isFocusAvailable, isOccluded);
        }


        public override void Draw(GameTime gameTime)
        {
            SpriteBatch.Begin();

            menu.Draw(SpriteBatch);

            SpriteBatch.End();
            
            base.Draw(gameTime);
        }

    }
}
